TARGET     := vita-test

TITLE_ID   := VITATEST1
TITLE_NAME := vita-test
APP_VER    := 01.00

APP_NAME_STR := vita-test
APP_VER_STR  := 1.00
APP_DIR_NAME := vita-test
BUILD_DATE   := $(shell date +"%Y.%m.%d")

MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
MAKEFILE_DIR  := $(dir $(MAKEFILE_PATH))
ROOT_DIR      := $(patsubst %/,%, $(MAKEFILE_DIR))

BUILD_DIR     := $(ROOT_DIR)/build
PKG_DIR       := $(ROOT_DIR)/pkg
SRC_DIR       := $(ROOT_DIR)/src
RELEASE_DIR   := $(ROOT_DIR)/release
RES_DIR       := res

VPK_NAME     := $(TARGET).vpk
VPK_PATH     := $(RELEASE_DIR)/$(VPK_NAME)
EBOOT_PATH   := $(PKG_DIR)/eboot.bin
SFO_PATH     := $(PKG_DIR)/sce_sys/param.sfo

SRC_C := \
	font_asc.c \
	font_chs.c \
	font.c \
	scr_print.c \
	config.c \
	utils.c \
	main.c

RES_FILES := \
	gamesd.skprx

SRC_OBJS := $(SRC_C:.c=.o)
RES_OBJS := $(RES_FILES:.skprx=.o)

OBJS := $(SRC_OBJS) $(RES_OBJS)
OBJS := $(addprefix $(BUILD_DIR)/, $(OBJS))

LINKS := 

LIBS := \
	-lSceDisplay_stub \
	-lSceCtrl_stub \
	-lSceProcessmgr_stub \
	-lSceAppUtil_stub \
	-lSceSysmem_stub \
	-lScePower_stub

INCLUDES := \
	-I$(SRC_DIR)

DEFINES := \
	-DAPP_NAME_STR=\"$(APP_NAME_STR)\" \
	-DAPP_VER_STR=\"$(APP_VER_STR)\" \
	-DAPP_DIR_NAME=\"$(APP_DIR_NAME)\" \
	-DBUILD_DATE=\"$(BUILD_DATE)\"

WARNINGS := \
	-Wall \
	-Wno-unused-variable \
	-Wno-unused-but-set-variable \
	-Wno-format-truncation

FLAGS += $(WARNINGS) $(INCLUDES) $(DEFINES)

LDFLAGS  := -Wl,-q

CFLAGS   += -O3 $(FLAGS)
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions

PREFIX  := arm-vita-eabi
CC      := $(PREFIX)-gcc
CXX     := $(PREFIX)-g++
AS      := $(PREFIX)-as
AR      := $(PREFIX)-ar
OBJCOPY := $(PREFIX)-objcopy
STRIP   := $(PREFIX)-strip
NM      := $(PREFIX)-nm
LD      := $(CXX) $(LINKS)

all: create_dir build-vita

build-vita: $(EBOOT_PATH) $(SFO_PATH)
	@mkdir -p $(dir $(VPK_PATH))
	@rm -rf $(VPK_PATH)
	cd $(PKG_DIR) && zip -r $(VPK_PATH) ./*

$(SFO_PATH):
	@mkdir -p $(dir $@)
	vita-mksfoex -s APP_VER="$(APP_VER)" \
		-s TITLE_ID="$(TITLE_ID)" "$(TITLE_NAME)" \
		-d ATTRIBUTE2=12 $@

$(EBOOT_PATH): $(BUILD_DIR)/$(TARGET).velf
	vita-make-fself -c $< $@

$(BUILD_DIR)/$(TARGET).velf: $(BUILD_DIR)/$(TARGET).elf
	$(STRIP) -g $<
	vita-elf-create $< $@

$(BUILD_DIR)/$(TARGET).elf: $(OBJS)
	$(LD) $(LDFLAGS) $(LIBDIRS) $^ $(LIBS) -o $@

$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
	$(CC) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/%.o: $(RES_DIR)/%.*
	$(PREFIX)-ld -r -b binary -o $@ $^

create_dir:
	@mkdir -p $(BUILD_DIR)

clean:
	rm -rf $(BUILD_DIR) $(VPK_PATH) $(EBOOT_PATH) $(SFO_PATH)

